//
//  MCQrCodeCollectionViewController.swift
//  StoreManage
//
//  Created by MC on 2018/5/17.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCQrCodeCollectionViewController: MCViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
     @objc override func leftItemClicked() {
        navigationController?.popViewController(animated: true)
    }
    
    @objc func rightItemClicked() {
        let vc = MCBillDetailViewController()
        
        let date = NSDate()
        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "yyyy-MM-dd"
        let strNowTime = timeFormatter.string(from: date as Date) as String
        
        vc.dateStr = strNowTime
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
    
    @objc func settingMoneyButtonClicked() {
        let vc = MCSettingMoneyViewController()
        vc.delegate = self
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
    
    @objc func saveQrCodeButtonClicked() {
        let image = MCImage_createdWithView(view: saveBgView)
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(savedPhotosAlbum(image:didFinishSavingWithError:contextInfo:)), nil)
    }
    @objc func savedPhotosAlbum(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: AnyObject) {
        
        if error != nil {
            MCHUD_text(message: "保存失败", view: self.view, delay: 1.5)
        } else {
            MCHUD_text(message: "已保存至相册", view: self.view, delay: 1.5)
        }
    }

    
    // MARK: - Network
    func sendNetworking() {
        
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "二维码收款"
        self.view.backgroundColor = UIColor.MCMain
        
        navigationBarLineHidden = true
        navigationBarTitleColor = UIColor.white
        navigationBarBackgroundImage = UIImage.init(color: UIColor.MCMain)
        
        
        navigationItem.leftBarButtonItem = MCSetBarButtonItem_image(#imageLiteral(resourceName: "Reuse_back").MCImageWithTintColor(color: UIColor.white), target: self, selector: #selector(leftItemClicked),isLeft: true)

        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("收款明细", textColor: UIColor.white,target: self, selector: #selector(rightItemClicked))
    }
    
    func initUI() {
        
        view.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(15)
            make.height.equalTo(460)
        }
        
        bgView.addSubview(saveBgView)
        saveBgView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(15)
            make.height.equalTo(380)
        }

        
        saveBgView.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(saveBgView)
            make.top.equalTo(saveBgView).offset(40)
        }
        
        saveBgView.addSubview(qrCodeImageView)
        qrCodeImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.top.equalTo(promptLabel.snp.bottom).offset(30)
            make.size.equalTo(CGSize.init(width: MCScreenWidth/2, height: MCScreenWidth/2))
        }
        
        
        saveBgView.addSubview(currentMoneyLabel)
        currentMoneyLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(saveBgView)
            make.top.equalTo(qrCodeImageView.snp.bottom).offset(10)
        }
        
        saveBgView.addSubview(wxImageView)
        wxImageView.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(qrCodeImageView.snp.centerX).offset(-10)
            make.top.equalTo(currentMoneyLabel.snp.bottom).offset(20)
            make.size.equalTo(CGSize.init(width: 40, height: 40))
        }
        
        saveBgView.addSubview(afbImageView)
        afbImageView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(qrCodeImageView.snp.centerX).offset(10)
            make.top.equalTo(currentMoneyLabel.snp.bottom).offset(20)
            make.size.equalTo(CGSize.init(width: 40, height: 40))
        }

        
        
        bgView.addSubview(lineOneView)
        lineOneView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.size.equalTo(CGSize.init(width: 1.5, height: 14))
            make.top.equalTo(saveBgView.snp.bottom).offset(20)
        }
        
        bgView.addSubview(settingMoneyButton)
        settingMoneyButton.snp.remakeConstraints { (make) ->Void in
            make.size.equalTo(CGSize.init(width: 80, height: 20))
            make.centerY.equalTo(lineOneView)
            make.centerX.equalTo(qrCodeImageView.snp.left)
        }
        
        bgView.addSubview(saveQrCodeButton)
        saveQrCodeButton.snp.remakeConstraints { (make) ->Void in
            make.size.equalTo(CGSize.init(width: 80, height: 20))
            make.centerY.equalTo(lineOneView)
            make.centerX.equalTo(qrCodeImageView.snp.right)
        }


        
    }
    
    // MARK: - Setter & Getter
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 5
        view.layer.masksToBounds = true
        return view
    }()
    
    
    lazy var saveBgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 5
        view.layer.masksToBounds = true
        return view
    }()

    lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.text = "支付宝、微信扫一扫,向我付钱"
        label.textColor = UIColor.MCWords
        label.font = MCFont(14)
        return label
    }()
    
    lazy var wxImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "SweepPay_wx")
        return imageView
    }()
    
    lazy var afbImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "SweepPay_zfb")
        return imageView
    }()
    
    lazy var qrCodeImageView: UIImageView = {
        let imageView = UIImageView()
        
        let storeStr = "storeId=" + String(MCUserinfo_getStoreId())
        let moneyStr = "&totalPrice=" + String(0)
        
        let url = H5_codeTopay + storeStr + moneyStr
        imageView.image = MCImage_createQRForString(qrString: url, iconImage: nil)
        return imageView
    }()
    
    lazy var currentMoneyLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(16)
        label.textColor = UIColor.MCMain
        return label
    }()
    
    lazy var settingMoneyButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.MCWords, for: .normal)
        button.setTitle("设置金额", for: UIControlState.normal)
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.right
        button.addTarget(self, action: #selector(settingMoneyButtonClicked), for: .touchUpInside)

        return button
    }()
    
    lazy var saveQrCodeButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.MCWords, for: .normal)
        button.setTitle("保存收款码", for: UIControlState.normal)
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
        button.addTarget(self, action: #selector(saveQrCodeButtonClicked), for: .touchUpInside)
        return button
    }()
    lazy var lineOneView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray
        return view
    }()
    
    lazy var lineTwoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
}

extension MCQrCodeCollectionViewController : MCSettingMoneyViewControllerDelegate {
    func MCSettingMoneyViewController_SettingMoney(moneyStr: String) {
        currentMoneyLabel.text = "¥ " + moneyStr
        
        let money = Double(moneyStr)!*100
        
        let storeStr = "storeId=" + String(MCUserinfo_getStoreId())
        let moneyStr = "&totalPrice=" + String(Int(money))
        let url = H5_codeTopay + storeStr + moneyStr
        qrCodeImageView.image = MCImage_createQRForString(qrString: url, iconImage: nil)
    }
}
